package com.adk.backend.service.impl;

import com.adk.backend.entity.LxOS;
import com.adk.backend.entity.LxOT;
import com.adk.backend.mapper.OSMapper;
import com.adk.backend.mapper.OTMapper;
import com.adk.backend.service.OrderRemarksService;
import com.adk.backend.service.OrderStatusService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 订单状态服务实现类
 */
@Slf4j
@Service
public class OrderStatusServiceImpl implements OrderStatusService {
    
    @Autowired
    private OSMapper osMapper;
    
    @Autowired
    private OTMapper otMapper;
    
    @Autowired
    private OrderRemarksService orderRemarksService;
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateOrderStatus(Long orderId, Integer status, Integer lastUpdateTime, Integer addUid, Integer addTime) {
        if (orderId == null || orderId == 0) {
            return false;
        }
        if (status == null || status == 0) {
            return false;
        }
        if (lastUpdateTime == null || lastUpdateTime == 0) {
            lastUpdateTime = (int) (System.currentTimeMillis() / 1000);
        }
        if (addTime == null || addTime == 0) {
            addTime = (int) (System.currentTimeMillis() / 1000);
        }
        
        try {
            // 查询是否已存在该订单的状态记录
            QueryWrapper<LxOS> wrapper = new QueryWrapper<>();
            wrapper.eq("o_id", orderId);
            LxOS existing = osMapper.selectOne(wrapper);
            
            if (existing != null) {
                // 如果新状态 <= 当前状态，则不更新
                if (existing.getStatus() != null && existing.getStatus() >= status) {
                    return false;
                } else {
                    // 更新状态
                    existing.setStatus(status);
                    existing.setLastStatusUpdateTime(lastUpdateTime);
                    return osMapper.updateById(existing) > 0;
                }
            } else {
                // 如果不存在，则插入新记录
                LxOS os = new LxOS();
                os.setOId(orderId);
                os.setStatus(status);
                os.setLastStatusUpdateTime(lastUpdateTime);
                os.setAddUid(addUid);
                os.setAddTime(addTime);
                return osMapper.insert(os) > 0;
            }
        } catch (Exception e) {
            log.error("更新订单状态失败: orderId={}, status={}", orderId, status, e);
            return false;
        }
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addOrderTracking(Long orderId, Integer statusCode, Integer statusTime, Integer addUid, Integer addTime) {
        if (orderId == null || orderId == 0) {
            return false;
        }
        if (statusCode == null || statusCode == 0) {
            return false;
        }
        if (statusTime == null || statusTime == 0) {
            statusTime = (int) (System.currentTimeMillis() / 1000);
        }
        if (addTime == null || addTime == 0) {
            addTime = (int) (System.currentTimeMillis() / 1000);
        }
        
        try {
            // 查询是否已存在该订单的状态记录
            QueryWrapper<LxOT> wrapper = new QueryWrapper<>();
            wrapper.eq("o_id", orderId);
            wrapper.eq("status_code", statusCode);
            LxOT existing = otMapper.selectOne(wrapper);
            
            if (existing != null) {
                // 如果存在，则更新is_show=1
                existing.setIsShow(1);
                return otMapper.updateById(existing) > 0;
            } else {
                // 如果不存在，则插入新记录
                LxOT ot = new LxOT();
                ot.setOId(orderId);
                ot.setStatusCode(statusCode);
                ot.setStatusTime(statusTime);
                ot.setAddUid(addUid);
                ot.setAddTime(addTime);
                ot.setIsShow(1); // 默认显示
                return otMapper.insert(ot) > 0;
            }
        } catch (Exception e) {
            log.error("添加订单跟踪状态失败: orderId={}, statusCode={}", orderId, statusCode, e);
            return false;
        }
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addOrderRemarks(Long orderId, Long sId, Integer status, Integer statusTime, 
                                   String rCn, String rEn, String rAr, Integer addTime, Integer addUid) {
        return orderRemarksService.addOrderRemarks(orderId, sId, status, statusTime, rCn, rEn, rAr, addTime, addUid);
    }
}
